package com.PMSys.Service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;



import com.PMSys.Common.DbUtils;
import com.PMSys.Dao.PostDao;
import com.PMSys.Dao.PostDaoImp;
import com.PMSys.Demain.Staff;
import com.PMSys.Demain.Post;

public class PostService {
	
	public void addPost(Post poster){
		try {
			
			Connection conn = DbUtils.getConnection();
		
			PostDao postDaoImp = new PostDaoImp(conn);
			postDaoImp.addPost(poster);
		
			DbUtils.commit();
		} catch (Exception e) {
			DbUtils.rollback();
		}
		finally{
		
			DbUtils.closeConnection();
		}
	}

	
		public Post getPostId(Post selectPoster){
			
			Connection conn = DbUtils.getConnection();
		
			PostDao postDaoImp = new PostDaoImp(conn);
			Post Poster = postDaoImp.getPostId(selectPoster);
			
		
			DbUtils.closeConnection();
			return Poster;
		}
		
		
		
		
		public void deletePost(String id){
			try {
				
				Connection conn = DbUtils.getConnection();
				
				PostDao postDaoImp = new PostDaoImp(conn);
				postDaoImp.deletePost(id);
				
				DbUtils.commit();
			} catch (Exception e) {
				
				DbUtils.rollback();
			}
			finally{
				
				DbUtils.closeConnection();
			}
		}
		
		

	
		
		public List<Post> getAllPost(){
			
		
			Connection conn = DbUtils.getConnection();
			
			PostDao postDaoImp = new PostDaoImp(conn);
			List<Post> Posts =  postDaoImp.getAllPost();
			
		
			DbUtils.closeConnection();
			return Posts;
		}

		public List<Staff> getStaff(Post poster) {
            Connection conn = DbUtils.getConnection();
			
			List<Staff> allPostStaff=new ArrayList<Staff>();
			
			PostDaoImp postDaoImp = new PostDaoImp(conn);
			allPostStaff = postDaoImp.getPostStaff(poster);
			
			
			DbUtils.closeConnection();
			return allPostStaff;
		}

		public void updatePost(Post poster) {
		
			try {
			
				Connection conn = DbUtils.getConnection();
			
				PostDao postDaoImp = new PostDaoImp(conn);
				postDaoImp.updatePost(poster);
				
				DbUtils.commit();
			} catch (Exception e) {
				DbUtils.rollback();
			}
			finally{
				
				DbUtils.closeConnection();
			}
		}
		
		
		
        public Post getPostbyId(Post selectPost) {
    		
			Connection conn = DbUtils.getConnection();
			
			PostDao postDaoImp = new PostDaoImp(conn);
			Post Poster = postDaoImp.getPostId(selectPost);
			
			
			DbUtils.closeConnection();
			return Poster;
		}
		

        
		public List<Post> getPostbyName(Post selectPost) {
			
			Connection conn = DbUtils.getConnection();
			
			PostDao postDaoImp = new PostDaoImp(conn);
			List<Post> allPoster = postDaoImp.getPostbyName(selectPost);
			
			
			DbUtils.closeConnection();
			return allPoster;
			
			
		}

		public List<Post> getPostbyType(Post selectPost) {
			
			Connection conn = DbUtils.getConnection();
			
			PostDao postDaoImp = new PostDaoImp(conn);
			List<Post> allPoster = postDaoImp.getPostbyType(selectPost);
			
			
			DbUtils.closeConnection();
			return allPoster;
		}


		public List<Post> selectByValue(Post post) {
           Connection conn = DbUtils.getConnection();
			
			PostDao postDaoImp = new PostDaoImp(conn);
			List<Post> allPoster = postDaoImp.selectByValue(post);
			
			
			DbUtils.closeConnection();
			return allPoster;
		}


}
